#!/usr/bin/env bash
# db-load -- Loads file to the MySQL database configured for a DeepDive application
# > db-load TABLE COLUMN FORMAT [SOURCE...]
##
set -eu

[[ $# -gt 0 ]] || usage "$0" "No TABLE given"
table=$1; shift
[[ $# -gt 0 ]] || usage "$0" "No COLUMNS given"
columns=$1; shift
[[ $# -gt 0 ]] || usage "$0" "No FORMAT given"
format=$1; shift

columns=${columns:+($columns)}

# load in parallel from multiple sources
do_load() {
    local sql=$1; shift
    local nsources=$#
    if [[ $nsources -gt 1 ]]; then
        local i=1
        for source; do
            show_progress input_to "loading $table [$i/$nsources]" -- \
            db-execute "$sql" <"$source" &
            let ++i
        done
        wait
    else
        source=$1; shift
        exec show_progress input_to "loading $table" -- \
        db-execute "$sql" <"$source"
    fi
}

# See: https://dev.mysql.com/doc/refman/5.7/en/load-data.html
case $format in
    tsv)
        do_load "LOAD DATA LOCAL INFILE '/dev/stdin' INTO TABLE $table
              FIELDS TERMINATED BY '\t'
              $columns
              " "$@"
        ;;
    csv)
        do_load "LOAD DATA LOCAL INFILE '/dev/stdin' INTO TABLE $table
              FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
              LINES TERMINATED BY '\r\n'
              $columns
              " "$@"
              # TODO if header=true, "IGNORE 1 LINES"
        ;;
    *) error "$format: unsupported format by MySQL driver" ;;
esac
